package com.example.courseapp.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.courseapp.BitmapUtils;
import com.example.courseapp.R;
import com.example.courseapp.bean.ChildDietaryRecords;
import com.example.courseapp.bean.FoodInfo;
import com.example.courseapp.utils.BigDecimalUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FoodRecordAdapter extends RecyclerView.Adapter<FoodRecordAdapter.ViewHolder> {

    List<ChildDietaryRecords> courseList = new ArrayList<>();


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_record_item, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ChildDietaryRecords records = courseList.get(position);
//        List<FoodInfo> infoList = new Gson().fromJson(records.getFood(), new TypeToken<List<FoodInfo>>() {
//        }.getType());

        List<FoodInfo> infoList = records.newFooldList;
        StringBuilder str = new StringBuilder();
        for (FoodInfo foodInfo : infoList) {
            if (foodInfo.number > 1) {
                String energy = BigDecimalUtils.mul(String.valueOf(foodInfo.getEnergy()), String.valueOf(foodInfo.number), 1);
                String weight = BigDecimalUtils.mul("100", String.valueOf(foodInfo.number), 1);
                str.append("\n").append(foodInfo.getFoodName())
                        .append("  " + weight + "g" + " ( 能量：" + energy + " )" + " x" + foodInfo.number);
            } else {
                str.append("\n").append(foodInfo.getFoodName()).append("  " + "100g ( 能量：" + foodInfo.getEnergy() + " )" + " x" + foodInfo.number);
            }
        }
        holder.textView.setText(records.getCreateTime() + str + "\n");
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void bindData(List<ChildDietaryRecords> courseList) {
        this.courseList.clear();
        this.courseList.addAll(courseList);
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}
